package com.qkl.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qkl.user.dto.*;
import com.qkl.user.dto.user.LevelStatDto;
import com.qkl.user.dto.user.UserProblemDto;
import com.qkl.user.service.InviteRelationService;
import com.qkl.user.service.MatrixService;
import com.qkl.user.service.UserProblemService;
import com.qkl.user.service.UserService;
import com.qkl.user.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("member")
public class MemberController {
     @Autowired
     private UserService userService;
     @Autowired
     private InviteRelationService inviteRelationService;
     @Autowired
     private UserProblemService userProblemService;
    @Autowired
    private MatrixService matrixService;
    /**
     * 个人信息
     */
     @GetMapping("info")
    public ResponseEntity<ExceptionResponse> info() {
          UserDto userDto = userService.getUserDtoId();
         return ResponseEntity.ok(ExceptionResponse.Success(userDto));
     }
    /**
     * 直推统计
     */
    @GetMapping("directStat")
    public ResponseEntity<ExceptionResponse> direct_stat() {
         InvitationUserDto integerMap =  inviteRelationService.directUsers();
         return ResponseEntity.ok(ExceptionResponse.Success(integerMap));
     }
    /**
     * 直推列表
     */
    @GetMapping("directList")
    public ResponseEntity<ExceptionResponse> direct_list(
            @RequestParam(required = false) String address,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<InvitationUserListDto> page = inviteRelationService.directList(address,pageNum, pageSize);
        return ResponseEntity.ok(ExceptionResponse.Success(page));
    }
    /**
     * 绑定地址
     */
    @PostMapping("bindAddress")
    public ResponseEntity<ExceptionResponse> bindAddress(@RequestParam("address") String address) {
        Boolean ok = userService.bindAddress(address);
        return ResponseEntity.ok(ExceptionResponse.Success(ok));
    }
    /**
     * 修改密码
     */
    @PostMapping("changePassword")
    public ResponseEntity<ExceptionResponse> changePassword(@RequestBody @Validated ChangePasswordDto changePasswordDto) {
         userService.changePassword(changePasswordDto);
        return ResponseEntity.ok(ExceptionResponse.Success());
    }
    /**
     * 个人问题
     */
    @GetMapping("userProblem")
    public ResponseEntity<ExceptionResponse> userProblem() {
         UserProblemDto userProblemDto = userProblemService.getUserProblem();
        return ResponseEntity.ok(ExceptionResponse.Success(userProblemDto));
    }
    /**
     * 节点总人数
     */
    @GetMapping("nodeAllCount")
    public ResponseEntity<ExceptionResponse> nodeAllCount() {
        long n = matrixService.queryTotalUserCount();
        return ResponseEntity.ok(ExceptionResponse.Success(n));
    }
    /**
     * 节点列表
     */
    @GetMapping("nodeList")
    public ResponseEntity<ExceptionResponse> nodeList( ) {
        List<LevelStatDto> list = matrixService.query16LevelUserCount();
        return ResponseEntity.ok(ExceptionResponse.Success(list));
    }

    /**
     * 地址是否在用户节点下面
     */
    @GetMapping("isInNode")
    public ResponseEntity<ExceptionResponse> isInNode(@RequestParam("address") String address) {

        boolean b = matrixService.isInNode(address);
        return ResponseEntity.ok(ExceptionResponse.Success(b));
    }

}
